//
//  LTY_SearchTableViewCell.m
//  MooMusic
//
//  Created by 守护神 on 15/10/26.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "LTY_SearchTableViewCell.h"
// 导入自定义collectionView的头问价
#import "LTY_SearchCollectionViewCell.h"

@implementation LTY_SearchTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatSearchSubViews];
    }
    
    return self;
}
// 创建视图
- (void)creatSearchSubViews
{
    // 创建一个布局信息对象  布局信息由flowout控制
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // item大小 (宽  高)
    flowLayout.itemSize = CGSizeMake(SCREENWIDTH / 5.5, 50);
    // 最小的行间距 (垂直于滚动方向的是一行)
    flowLayout.minimumLineSpacing = 3;
    // 最小列间距 (每一行item的间距)
    // 最小的列间距  cell的大小 section的缩进 共同来决定的item之间的距离
    flowLayout.minimumInteritemSpacing = 1;
    // section的缩进 (上 左 下 右)
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);

#pragma mark -- UICollectionView
    CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 120) collectionViewLayout:flowLayout];
    CollectionView.backgroundColor = [UIColor whiteColor];
    
    CollectionView.delegate = self;
    CollectionView.dataSource = self;
    
    // 注册item样式
    [CollectionView registerClass:[LTY_SearchCollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
    
    // 添加在视图上
    [self.contentView addSubview:CollectionView];
    
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 必须要重新刷新一遍collectionview
    if (self.tempDataArray.count > 0) {
        // 刷新collectionview
        [CollectionView reloadData];
    }
}


#pragma mark -- UICollectionViewDataSource
// 每个section中的item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
    
}


// item重用池
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LTY_SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    
    if (self.tempDataArray.count > 0) {
        // 把model传递过去
        cell.model = [self.tempDataArray objectAtIndex:indexPath.item];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
    
}

// collectionView点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"collectionviewItem的点击事件");
    NSString *str = [[self.tempDataArray objectAtIndex:indexPath.item] valueForKey:@"val"];
    // 发送一个通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PushNextViewController" object:str userInfo:nil];
   
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
